﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLDAOL.Models;
using QLDAOL.ModelView;
using System.IO;
namespace QLDAOL.Controllers
{
    public class DownloadController : BaseController
    {
        //
        // GET: /Download/
        private IEQLDAOLRepository db = new IEQLDAOLRepository();
        public ActionResult download(int pjid,string username)
        {
            IEnumerable<DOWNLOAD> dl ;
            if (username == "")
                dl = db.getAllDownload(pjid);
            else
                dl = db.getAllDownloadbyuser(username);
            if (dl != null)
            {
                ViewData["date"] = Util.Util.ConvertToUnixTimestamp(DateTime.Now);
                ViewData["kt"] = "co";
                ViewData["pjid"] = pjid;
                var resutl = from c in dl
                             select new DownloadModel
                             {
                                 id = c.ID,
                                 Title = c.Title,
                                 file_path = c.File_Path,
                                 user = c.UserName,
                                 update_date = (DateTime)c.Date
                             };
                return View(resutl);
            } 
             else
            {
                ViewData["kt"] = "khongco";
                return View();
            }
        }
        public DownloadResult GetDownloadFile(string filename)
        {
            return new DownloadResult
            {
                FileName = filename,
                Path = "~/Download/" + filename
            };
        }
        [HttpGet]
        public ActionResult Uploadfile()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Uploadfile(string dl_title)
        {
            if (Request.Files != null)
            {
                var posted = Request.Files["dldocument"];
                if (posted != null && posted.FileName != "" && posted.ContentLength <= 5120000)//5000kb
                {
                    const string pathStoredImage = "/Download/";
                    var imageName = Path.GetFileName(posted.FileName);
                    var filePath = pathStoredImage + imageName;
                    posted.SaveAs(Server.MapPath(filePath));
                    string user = "";
                    if (Session["user_logon_name"] != null)
                        user = Session["user_logon_name"].ToString();
                    else
                        user = "admin";
                    int pjid =(int) Session["pjID"];
                    DOWNLOAD newdl = new DOWNLOAD
                    {
                        Title = dl_title,
                        File_Path = imageName,
                        UserName = user,
                        Date = DateTime.Now,
                        Project_Id=pjid

                    };
                    db.addtoDownload(newdl);
                    return View();

                }
                else
                    return RedirectRegister();
            }


           
            return View();
        }
    }
}
